package com.blog.backend.service.impl;

import com.blog.backend.config.CodeMsg;
import com.blog.backend.config.RedisValueConfig;
import com.blog.backend.exception.GlobalException;
import com.blog.backend.mapper.ImageMapper;
import com.blog.backend.mapper.TravelManagerMapper;
import com.blog.backend.req.AddTravelReq;
import com.blog.backend.req.TravelReq;
import com.blog.backend.resp.FoodResp;
import com.blog.backend.resp.PictureUrlResp;
import com.blog.backend.resp.TravelResp;
import com.blog.backend.service.TravelManageService;
import com.blog.backend.utils.PageUtils;
import com.blog.backend.utils.RedisUtils;
import com.blog.backend.utils.UuidUtils;
import com.blog.backend.vo.PageRequest;
import com.blog.backend.vo.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class TravelManageServiceImpl implements TravelManageService {

    @Autowired
    private TravelManagerMapper travelManagerMapper;

    @Autowired
    private ImageMapper imageMapper;

    @Resource
    private RedisUtils redisUtils;
    @Override
    public PageResult<TravelResp> selectTravelList(TravelReq req) {
        log.info("游记列表查询：{}",req.getTravelName());
        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        List<TravelResp> list=travelManagerMapper.selectTravelList(req);
        if (!CollectionUtils.isEmpty(list)){
            for (TravelResp travelResp : list) {
                PictureUrlResp pictureUrlResp = imageMapper.selectImage(travelResp.getTravelImg());
               List<PictureUrlResp> respList= new ArrayList<>();
                respList.add(pictureUrlResp);
                travelResp.setTravelImageList(respList);
            }
        }
        PageInfo<TravelResp> info=new PageInfo<TravelResp>(list);
        PageRequest pageRequest = new PageRequest();
        return PageUtils.getPageResult(pageRequest, info);
    }

    @Override
    public Boolean insertTravel(AddTravelReq req) {
        log.info("新增游记内容：{}",req.getTravelTitle());
        req.setTravelId(UuidUtils.getId());
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        req.setTravelTimeStr(format.format(req.getTravelTime()));
        travelManagerMapper.insertTravel(req);
        return true;
    }

    @Override
    public TravelResp selectTravelDetail(String travelId) {
        log.info("查看游记详情：{}",travelId);
        TravelResp resp= travelManagerMapper.selectTravelDetail(travelId);
        if (resp==null){
            throw new GlobalException(CodeMsg.FOOD_PARAMETER_ERROR);
        }
        if (StringUtils.isNotBlank(resp.getTravelImg())){
            List<PictureUrlResp> strings = imageMapper.selectImageList(resp.getTravelImg());
            resp.setTravelImageList(strings);
        }
        if (redisUtils.hasKey(RedisValueConfig.article_pre + travelId)){
            Integer browse = (Integer) redisUtils.get(RedisValueConfig.article_pre + travelId);
            resp.setBrowseNum(browse);
        }

        return resp;
    }

    @Override
    public Boolean updateTravelDetail(AddTravelReq req) {
        log.info("修改的游记：{}",req.getTravelId());
        TravelResp resp= travelManagerMapper.selectTravelDetail(req.getTravelId());
        if (resp==null){
            throw new GlobalException(CodeMsg.FOOD_PARAMETER_ERROR);
        }
        travelManagerMapper.updateTravelDetail(req);
        return true;
    }

    @Override
    public Boolean deleteTravel(String travelId) {
        log.info("删除的游记：{}",travelId);
        TravelResp resp= travelManagerMapper.selectTravelDetail(travelId);
        if (resp==null){
            throw new GlobalException(CodeMsg.FOOD_PARAMETER_ERROR);
        }
        travelManagerMapper.updateTravelStatus(travelId);
        //删除相关图片
        imageMapper.updateImageStatus(resp.getTravelImg());
        return true;
    }

    @Override
    public List<TravelResp> getHotTravelList() {
        List<TravelResp> list= travelManagerMapper.getHotTravelList();
        if (!CollectionUtils.isEmpty(list)){
            for (TravelResp travelResp : list) {
                PictureUrlResp pictureUrlResp = imageMapper.selectImage(travelResp.getTravelImg());
                List<PictureUrlResp> respList= new ArrayList<>();
                respList.add(pictureUrlResp);
                travelResp.setTravelImageList(respList);
            }
        }
        return list;
    }
}
